package com.ww.springboot.boot.thread.study.base.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * 线程只要设置了中断标识 可中断方法执行时就会触发-》抛出异常
 * 如果中断信号被获取 中断标识被重置 则后面的中断方法不会在被触发
 *
 * @author wanwei
 * @since 2021-04-17 15:46
 */
public class InterruptDemo6 {

    public static void main(String[] args) {
        //① 判断当前线程是否被中断
        System.out.println("Main thread is interrupted? " + Thread.interrupted());
        //②中断当前线程
        Thread.currentThread().interrupt();
        //③判断当前线程是否已经被中断
        System.out.println("Main thread is interrupted? " + Thread.currentThread().isInterrupted());
        try {
            //④ 当前线程执行可中断方法
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            //⑤捕获中断信号
            System.out.println("I will be interrupted still.");
        }

        //中断标识在上面的方法已经被重置
        try {
            //④ 当前线程执行可中断方法
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            //⑤捕获中断信号
            System.out.println("I will be interrupted still.");
        }
    }
}
